"""
Case Type   : 资源池化集群支持多链路
Case Name   : dn主、cm主在同一节点，主节点链路1隔断
Create At   : 2025/04/13
Owner       : @kyleze
Description :
    1.构建主dn主、cm主在同一节点
    2.主节点链路1隔断
    3.查看集群状态
    4.恢复链路，恢复集群
    5.查看集群状态
Expect      :
    1.成功
    2.成功
    3.集群状态正常
    4.成功
    5.集群正常
History     :
"""
import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread

from yat.test import Node
from yat.test import macro


class MultipathTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pri_user_node = Node('PrimaryDbUser')
        self.pri_root = Node(node="PrimaryRoot")
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.sta1_root = Node(node="Standby1Root")
        self.sta1_user_node = Node('Standby1DbUser')
        self.sta2_sh = CommonSH('Standby2DbUser')
        self.sta2_root = Node(node="Standby2Root")
        self.sta2_user_node = Node('Standby2DbUser')
        self.ip_1 = macro.PRIMARY_LUN_IP.split()[0].strip()
        self.ip_2 = macro.STANDBY1_LUN_IP.split()[0].strip()
        self.ip_3 = macro.STANDBY2_LUN_IP.split()[0].strip()
        self.log.info("----获取主节点网络设备名称----")
        self.pri_network_card = ''
        interfaces = self.pri_root.sh("ip -o addr show").result()
        for item in interfaces.split('\n'):
            if self.ip_1 in item:
                self.pri_network_card = item.split(' ')[1].split(':')[0]
        self.log.info(self.pri_network_card)
        self.log.info("----获取备1节点网络设备名称----")
        self.st1_network_card = ''
        interfaces = self.sta1_root.sh("ip -o addr show").result()
        for item in interfaces.split('\n'):
            if self.ip_2 in item:
                self.st1_network_card = item.split(' ')[1].split(':')[0]
        self.log.info(self.st1_network_card)
        self.log.info("----获取备2节点网络设备名称----")
        self.st2_network_card = ''
        interfaces = self.sta2_root.sh("ip -o addr show").result()
        for item in interfaces.split('\n'):
            if self.ip_3 in item:
                self.st2_network_card = item.split(' ')[1].split(':')[0]
        self.log.info(self.st2_network_card)

    def test_multipath(self):
        step = '--step1:构建主dn主、cm主在同一节点 expect:成功--'
        self.log.info(step)
        query_res = self.primary_sh.get_db_cluster_status('detail')
        self.log.info(query_res)
        self.assertIn('Normal', query_res, '执行失败:' + step)
        self.cm_sever_id = ''
        for line in query_res.split('\n'):
            if "cm_server Primary" in line:
                self.cm_sever_id = line.split(" ")[0]
        if self.cm_sever_id == '1':
            self.log.info("cm_server Primary在主节点")
        elif self.cm_sever_id == '2':
            switchover_res = \
                self.primary_sh.exec_cm_ctl('switchover', '-n 2 -D $PGDATA')
            self.log.info(switchover_res)
            self.assertIn(self.constant.cm_switchover_success_msg,
                          switchover_res, '执行失败:' + step)
        else:
            switchover_res = \
                self.primary_sh.exec_cm_ctl('switchover', '-n 3 -D $PGDATA')
            self.log.info(switchover_res)
            self.assertIn(self.constant.cm_switchover_success_msg,
                          switchover_res, '执行失败:' + step)

        step = '--step2:主节点链路1隔断 expect:成功--'
        self.log.info(step)
        if self.cm_sever_id == '1':
            self.log.info("----主节点断开链路1----")
            res = self.pri_root.sh(f'ifdown {self.pri_network_card}').result()
            self.log.info(res)
            self.assertIn("successfully deactivated", res, "执行失败" + step)
        elif self.cm_sever_id == '2':
            self.log.info("----备1节点断开链路1----")
            res = self.sta1_root.sh(f'ifdown {self.st1_network_card}').result()
            self.log.info(res)
            self.assertIn("successfully deactivated", res, "执行失败" + step)
        else:
            self.log.info("----备2节点断开链路1----")
            res = self.sta2_root.sh(f'ifdown {self.st2_network_card}').result()
            self.log.info(res)
            self.assertIn("successfully deactivated", res, "执行失败" + step)

        step = '--step3:查看集群状态 expect:集群状态正常--'
        self.log.info(step)
        query_res = self.primary_sh.get_db_cluster_status('status')
        self.log.info(query_res)
        self.assertTrue(query_res, '执行失败:' + step)

        step = '--step4:恢复链路 expect:成功--'
        self.log.info(step)
        if self.cm_sever_id == '1':
            self.log.info("----主节点断开链路1----")
            res = self.pri_root.sh(f'ifup {self.pri_network_card}').result()
            self.log.info(res)
            self.assertIn("successfully activated", res, "执行失败" + step)
        elif self.cm_sever_id == '2':
            self.log.info("----备1节点断开链路1----")
            res = self.sta1_root.sh(f'ifup {self.st1_network_card}').result()
            self.log.info(res)
            self.assertIn("successfully activated", res, "执行失败" + step)
        else:
            self.log.info("----备2节点断开链路1----")
            res = self.sta2_root.sh(f'ifup {self.st2_network_card}').result()
            self.log.info(res)
            self.assertIn("successfully activated", res, "执行失败" + step)
        start_res = self.primary_sh.exec_cm_ctl("switchover", '-a')
        self.log.info(start_res)
        self.assertIn(self.constant.cm_switchover_success_msg, start_res,
                      '执行失败' + step)

        step = '--step5:查看集群状态 expect:成功--'
        self.log.info(step)
        query_res = self.primary_sh.get_db_cluster_status('status')
        self.log.info(query_res)
        self.assertTrue(query_res, '执行失败:' + step)

    def tearDown(self):
        self.log.info(f'----{os.path.basename(__file__)} end----')

